package cn.db101.fairy.service

import cn.db101.fairy.entity.FairyCategory
import cn.db101.fairy.entity.FairyDetail
import cn.db101.fairy.entity.UserCollect
import cn.db101.fairy.mapper.FairyDetailMapper
import cn.db101.fairy.model.FairyDetailParam
import com.mybatisflex.core.query.QueryColumn
import com.mybatisflex.core.query.QueryMethods.select
import com.mybatisflex.core.query.QueryWrapper
import com.mybatisflex.kotlin.extensions.db.paginate
import com.mybatisflex.kotlin.extensions.db.query
import com.mybatisflex.kotlin.extensions.db.queryOne
import com.mybatisflex.kotlin.extensions.kproperty.eq
import com.mybatisflex.kotlin.extensions.kproperty.like
import com.mybatisflex.kotlin.extensions.wrapper.whereWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class FairyService {

    @Autowired
    lateinit var fairyDetailMapper: FairyDetailMapper

    fun all(): List<FairyCategory> {

        return query {}
    }

    /**
     * 分页查询
     */
    fun detailList(fairyDetailParam: FairyDetailParam): List<FairyDetail> {

        val paginateWith = paginate<FairyDetail>(pageNumber = fairyDetailParam.current,
            pageSize = fairyDetailParam.size,
            init = {
                select(QueryColumn("id"), QueryColumn("name"), QueryColumn("category_id"))
                whereWith {
                    FairyDetail::categoryId eq fairyDetailParam.categoryId
                }
            }
        )

        return paginateWith.records
    }

    fun detail(id: Int?): FairyDetail? {

        return queryOne {
            whereWith {
                FairyDetail::id eq id!!
            }
        }

    }

    fun findTop6Views(): List<FairyDetail> {

        return fairyDetailMapper.findTop6Views()
    }

    fun findTop6Collects(): List<FairyDetail> {

        return fairyDetailMapper.findTop6Collects()
    }

    fun page(fairyDetailParam: FairyDetailParam): Any {

        val paginateWith = paginate<FairyDetail>(pageNumber = fairyDetailParam.current,
            pageSize = fairyDetailParam.size,
            init = {
                select(QueryColumn("id"),
                    QueryColumn("name"),
                    QueryColumn("title"),
                    QueryColumn("category_id"),
                    QueryColumn("views"),
                    QueryColumn("collects"),
                    QueryColumn("thumbnail")
                )
                orderBy(FairyDetail::views, false)
            }
        )

        return paginateWith.records
    }

    fun addViews(detail: FairyDetail) {

        fairyDetailMapper.addViews(detail.id)
    }

    fun collectList(collect: UserCollect): List<FairyDetail> {

        // 查询用户的收藏id
        val collectList = query<UserCollect> {
            whereWith {
                UserCollect::userId eq collect.userId
            }
        }

        // 通过列表查询童话列表
        return fairyDetailMapper.selectListByQuery(QueryWrapper.create().`in`(FairyDetail::id, collectList.map { it.detailId }))
    }

    fun search(detail: FairyDetail): List<FairyDetail> {
        return query<FairyDetail> {
            whereWith {
                FairyDetail::name like detail.name!!
            }
        }
    }

}
